package k8.util;

import java.nio.ByteBuffer;

import k8.Texture;

import org.lwjgl.opengl.GL11;

public abstract class ProceduralTexture extends Texture {

    /** Creates a new instance of ProceduralTexture */
    public ProceduralTexture(int width, int height) {
        float xdelta = (float) 2 / width;
        float ydelta = (float) 2 / height;
        int c[] = new int[3];
        float x, y;

        // Create a buffer to hold width * height RGB values
        ByteBuffer pixel_buffer = ByteBuffer.allocateDirect(3 * width * height);

        // Get colour for each element of the texture
        for (y = -1; y < 1; y += ydelta) {
            for (x = -1; x < 1; x += xdelta) {
                colourAt(x, y, 0, c);
                pixel_buffer.put((byte) c[0]).put((byte) c[1]).put((byte) c[2]);
            }
        }

        // Generate the texture
        generate(pixel_buffer, width, height, GL11.GL_RGB4);
    }

    /** Calculates the colour at the specified location <x, y, z> */
    abstract protected void colourAt(float x, float y, float z, int[] colour);

}
